//
//  KFBannerView.m

//
//  Created by kongfei on 15/11/30.

//

#import "KFBannerView.h"

@interface KFBannerView ()<UIScrollViewDelegate>

@property (strong, nonatomic) UIPageControl *pageControl;

@property (strong, nonatomic) NSMutableArray *curArray;

@property (strong, nonatomic) NSTimer *timer;

@end

@implementation KFBannerView{
    CGFloat width;
    CGFloat height;
    NSInteger curPage;
//    BOOL isTimeUp;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        width = frame.size.width;
        height = frame.size.height;
        self.curArray = [NSMutableArray array];
        self.contentSize = CGSizeMake(width * 3, height);
        self.contentOffset = CGPointMake(width, 0);
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        self.pagingEnabled = YES;
        self.delegate = self;
        [self createBannerView];
        [self createPageControl];
//        isTimeUp = NO;
        
    }
    return self;
}

- (void)createBannerView{
    for (int i = 0; i < 3; i ++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(width * i, 0, width, height)];
        [self addSubview:imageView];
    }
}

- (void)createPageControl{
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake((width - 80)/2+width, height - 30, 80, 30)];
    self.pageControl.currentPage = 1;
    [self addSubview:_pageControl];
}

//  更新当前视图对应的索引
- (NSInteger)updatePage:(NSInteger)page{
    NSInteger count = _imageArray.count;
    return (count + page) % count;
}

//  更新当前对应的视图

- (void)updateCurrentViewWithCurPage:(NSInteger)page{
    NSInteger pre = [self updatePage:page - 1];
    curPage = [self updatePage:page];
    NSInteger last = [self updatePage:page + 1];
    
    [self.curArray removeAllObjects];
    [self.curArray addObject:_imageArray[pre]];
    [self.curArray addObject:_imageArray[curPage]];
    [self.curArray addObject:_imageArray[last]];
    
    NSArray *subArray = self.subviews;
    for (int i = 0; i < 3; i++) {
        UIImageView *imageView = subArray[i];
        imageView.image = [UIImage imageNamed:_curArray[i]];
    }
    
    self.contentOffset = CGPointMake(width, 0);
    self.pageControl.currentPage = curPage;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat x = self.contentOffset.x;
    if (x >= width * 2) {
        [self updateCurrentViewWithCurPage:curPage + 1];
    }
    else if (x <= 0){
        [self updateCurrentViewWithCurPage:curPage - 1];
    }
//    if (!isTimeUp) {
//        [self.timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2]];
//    }
//    isTimeUp = NO;
    
}


- (void)setImageArray:(NSArray *)imageArray{
    _imageArray = imageArray;
    self.pageControl.numberOfPages = imageArray.count;
    [self updateCurrentViewWithCurPage:0];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(autoPlay) userInfo:nil repeats:YES];
}

- (void)autoPlay{
//    isTimeUp = YES;
    [self setContentOffset:CGPointMake(width * 2, 0) animated:YES];
}


@end
